package cn.smbms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.BillService;
import cn.smbms.service.ProviderService;
import cn.smbms.tools.Constants;
import cn.smbms.tools.PageSupport;


@Controller
@RequestMapping(value="/sys/bill")
public class BillController {
	@Resource
	private BillService billService;
	@Resource
	private ProviderService providerService;
	
	
	@RequestMapping("/billlist")
	public String getAllBill(Model model,
			@RequestParam(value="queryProductName",required=false) String queryProductName,
			@RequestParam(value="queryProviderId",required=false) String queryProviderId,
			@RequestParam(value="queryIsPayment",required=false) String queryIsPayment,
			//@RequestParam(value="queryProName",required=false) String queryProName,
			//@RequestParam(value="queryProCode",required=false) String queryProCode,
			@RequestParam(value="pageIndex",required=false) String pageIndex){
		Integer _queryProviderId = null;	
		Integer _queryIsPayment = null;
		List<Bill> billList = null;
		List<Provider> providerList = null;
		//设置页面容量
    	int pageSize = Constants.pageSize;
    	//当前页码
    	int currentPageNo = 1;
    	if(queryProductName==null) {
    		queryProductName="";
		}
		
		if(queryProviderId !=null && !queryProviderId.equals("")) {
			_queryProviderId=Integer.parseInt(queryProviderId);
		}
		if(queryIsPayment != null && !queryIsPayment.equals("")){
			_queryIsPayment = Integer.parseInt(queryIsPayment);
		}
		
		if(pageIndex!=null) {
			try {
			currentPageNo=Integer.parseInt(pageIndex);
			}catch(NumberFormatException e) {
				return "redirect:/syserror";
			}
		}
		//总数量
		int totalCount=billService.getBillCount(queryProductName, _queryProviderId, _queryIsPayment);
		PageSupport pages=new PageSupport();
		pages.setCurrentPageNo(0);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);
		int totalPageCount =pages.getTotalCount();
		if(totalCount/pageSize!=0||totalCount<pageSize){
			totalPageCount=totalCount/pageSize+1;
		}else {
			totalPageCount=totalCount/pageSize;
		}
		//控制首页和尾页	
		if(currentPageNo<1) {
			currentPageNo=1;
		}else if(currentPageNo>totalPageCount) {
			currentPageNo=totalPageCount;
		}
		billList=billService.getBillList(queryProductName, _queryProviderId, _queryIsPayment, currentPageNo, pageSize);
		//providerList = providerService.getProviderList(queryProductName, queryProviderId, pageSize, currentPageNo);
		
		providerList = providerService.getProList();
		model.addAttribute("billList", billList);
		model.addAttribute("providerList", providerList);
		model.addAttribute("queryProductName", queryProductName);
		model.addAttribute("queryProviderId", queryProviderId);
		model.addAttribute("queryIsPayment", queryIsPayment);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("currentPageNo", currentPageNo);
		return "billlist";
	}
	
	//删除
	
	@RequestMapping(value="delbill.json",method = RequestMethod.GET)
	@ResponseBody
	public Object delBill(@RequestParam String id){
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(StringUtils.isNullOrEmpty(id)){
			resultMap.put("delResult", "notexist");
		}else{
			try {
				if(billService.deleteBillById(Integer.parseInt(id))){
					resultMap.put("delResult", "true");
				}else { 
					resultMap.put("delResult", "false");
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return JSONArray.toJSONString(resultMap);	
	}
	
	//修改
	@RequestMapping(value="update/{id}", method = RequestMethod.GET)
	public String updateBill(@PathVariable("id") int id,HttpServletRequest request,HttpSession session){
		Bill bill=billService.getBillById(id);
		request.setAttribute("bill", bill);	
		return "billmodify";
	}	
	@RequestMapping(value="update", method = RequestMethod.POST)
	public String doUpdate(Bill bill,User user,HttpServletRequest request,HttpSession session){	
		bill.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		bill.setModifyDate(new Date());
		int i=this.billService.modify(bill);
		if(i>0) {
			return "redirect:/sys/bill/billlist";
		}else {
			return "billmodify";
		}		
	}
	
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String doAdd(@ModelAttribute("bill") Bill bill,HttpServletRequest request,HttpSession session) {
		request.setAttribute("bill", bill);
		return "billadd";
	}
	//添加订单
	@RequestMapping(value = "addsave", method = RequestMethod.POST)
	public String add(Bill bill,HttpSession session) {
		bill.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
		bill.setCreationDate(new Date());
		int i=billService.add(bill);
		if(i>0) {
			return "redirect:/sys/bill/billlist";
		}
		return "billadd";
	}
	
		
	
	//具体内容
	/*@RequestMapping(value="view.html",method=RequestMethod.GET)
	@ResponseBody
	public Object view(@PathVariable String id) {
		String cjson="";
		if(null==id||"".equals(id)) {
			return "";
		}else {
			try {
				Bill bill=billService.getBillById(Integer.parseInt(id));
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
			return cjson;
	}*/
	@RequestMapping(value="view/{id}",method=RequestMethod.GET)
	public String view (@PathVariable int id,Model model) {
		Bill bill=billService.getBillById((id));
		model.addAttribute(bill);
		return "billview";
		
	}
	
	//
	@RequestMapping(value="/providerlist.json",method=RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public List<Provider> getProviderList(){
		List<Provider> providerList = null;
		try {
			providerList = providerService.getProList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return providerList;
	}
}
